package com.punch.oauth.controller;


import com.punch.common.model.Result;
import com.punch.oauth.service.ValidateCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 验证控制层
 */
@Controller
@Api(value = "ValidateController", tags = "验证码控制层")
public class ValidateController {

    @Resource
    private ValidateCodeService validateCodeService;

    /**
     * 发送手机验证码
     * 后期要加接口限制
     *
     * @param mobile 手机号
     * @return R
     */
    @GetMapping("/oauth/mobile/code/{mobile}")
    @ResponseBody
    @ApiOperation(value = "发送手机验证码")
    public Result<Boolean> createCode(@PathVariable String mobile, HttpServletRequest request) {
        Assert.notNull(mobile, "手机号不能为空");
        return validateCodeService.sendSmsCode(mobile, request);
    }

    /**
     * 微信验证token是否为空
     *
     * @param authorizationToken token
     * @return true/false
     */
    @ResponseBody
    @GetMapping("/oauth/wx/check/token")
    public Result<Boolean> wxCheckToken(@RequestParam("authorizationToken") String authorizationToken) {
        Assert.notNull(authorizationToken, "token不能为空");
        return validateCodeService.wxCheckToken(authorizationToken);
    }

    /**
     * 生成图片验证码
     *
     * @author wujj
     * @since 2020/6/5 18:35
     */
    @RequestMapping(value = "/oauth/captcha", produces = MediaType.IMAGE_PNG_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = " 生成图片验证码", notes = " 生成图片验证码", produces = MediaType.IMAGE_PNG_VALUE)
    public void captchaCode(HttpServletRequest request, HttpServletResponse response) {
        validateCodeService.captchaCode(request, response);
    }

}
